﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class StructrueInformationController : Controller
    {
        private StructrueInformationInfo groinfo = new StructrueInformationInfo();
        public ActionResult StructrueInformationIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取岗位列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetList(PageVM pvm)
        {
            string query = " and 1=1 ";
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                if (param[0] != "")
                {
                    query += " and structrueName like '%" + param[0] + "%'";
                }
            }
            int count;
            IList<StructrueInformationInfo> grolist = groinfo.SelInfo(pvm.rp, pvm.page, query, out count);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = count;
            List<string> v;
            foreach (StructrueInformationInfo item in grolist)
            {
                #region
                v = new List<string>();
                v.Add(item.structrueId.ToString());
                v.Add(item.structrueName);
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.structrueId.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        public ActionResult AddStructrueInformation()
        {
            return View();
        }

        /// <summary>
        /// 保存班组（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveStructrueInformation(StructrueInformationVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                StructrueInformationInfo gro = new StructrueInformationInfo()
                {
                    structrueName = model.structrueName
                };
                result = groinfo.SaveStructrueInformation(gro);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditStructrueInformation(string id)
        {
            StructrueInformationVM vm = new StructrueInformationVM();
            StructrueInformationInfo info = new StructrueInformationInfo();
            groinfo = info.GetStructrueInformationInfoOne(id);
            vm.structrueId = groinfo.structrueId;
            vm.structrueName = groinfo.structrueName;
            //Mapper.DynamicMap(groinfo, vm);
            return View(vm);
        }

        /// <summary>
        /// 保存岗位（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveStructrueInformationEdit(StructrueInformationVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                StructrueInformationInfo gro = new StructrueInformationInfo()
                {
                    structrueId = model.structrueId,
                    structrueName = model.structrueName
                };
                result = groinfo.SaveStructrueInformationEdit(gro);
            }
            return Json(result);
        }

        /// <summary>
        /// 删除岗位
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteStructrueInformation(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = groinfo.DeleteStructrueInformation(strId);
            return Json(result);
        }
    }
}
